/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.util;

import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;


public class TimeUtil {
  private static final Map<String, Long> CONVERSION_FACTORS = makeConversionFactorsMap();

  private static HashMap<String, Long> makeConversionFactorsMap() {
    HashMap<String, Long> map = new HashMap<String, Long>();
    long seconds = 1000;
    long minutes = seconds * 60;
    long hours = minutes * 60;
    long days = hours * 24;
    long weeks = days * 7;

    map.put("seconds", seconds);
    map.put("minutes", minutes);
    map.put("hours", hours);
    map.put("days", days);
    map.put("weeks", weeks);
    return map;
  }

  public static long toMillis(double amount, String sourceType) {
    return (long) (amount * CONVERSION_FACTORS.get(StringUtils.lowerCase(sourceType)));
  }

  public static double fromMillis(long milliseconds, String destinationType) {
    return ((double) milliseconds) / CONVERSION_FACTORS.get(StringUtils.lowerCase(destinationType));
  }
}
